using UnityEngine;
using UnityEngine.Rendering;

public class BodyPart : MonoBehaviour
{
	public Body body;

	public float damageRatio = 1f;

	public Sprite[] bloodSprites;

	public AudioClip[] hitAudios;

	[HideInInspector]
	public Rigidbody2D rb;

	[HideInInspector]
	public HingeJoint2D hj;

	[HideInInspector]
	public Collider2D col;

	[HideInInspector]
	public SpriteRenderer bloodSpriteRenderer;

	[HideInInspector]
	public bool dieForceAdded;

	public int hitCount;

	public float damageCount;

	public float damageShare;

	public float resistance = 100f;

	public float breakAfterDamageShare = 1f;

	public BodyPart[] childBodyParts;

	public float brokenChance;

	[Range(0f, 1f)]
	public float knockbackChance = 0.2f;

	public ParticleSystem onHitEffect;

	public bool isFireproof;

	public BodyPartFire onFire;

	private void Awake()
	{
		rb = GetComponent<Rigidbody2D>();
		hj = GetComponent<HingeJoint2D>();
		col = GetComponent<Collider2D>();
		if ((bool)base.transform.Find("Blood"))
		{
			bloodSpriteRenderer = base.transform.Find("Blood").GetComponent<SpriteRenderer>();
		}
		if ((bool)body && (bool)body.zombie && Random.Range(0f, 1f) > 0.7f)
		{
			damageCount = body.zombie.health * 0.1f;
			damageShare = damageCount / body.zombie.health;
			UpdateBloodSpriteRenderer();
		}
	}

	private void Start()
	{
		if (brokenChance > Random.Range(0f, 1f))
		{
			base.gameObject.name += "Broken";
			rb.isKinematic = false;
			BodyPart[] array = childBodyParts;
			foreach (BodyPart obj in array)
			{
				obj.name += "Broken";
				obj.rb.isKinematic = false;
			}
			body.zombie.animator.Rebind();
		}
	}

	public void Bleed(Vector2 pos)
	{
		if (!onHitEffect)
		{
			return;
		}
		ParticleSystem[] componentsInChildren = Object.Instantiate(onHitEffect, pos, Quaternion.identity).GetComponentsInChildren<ParticleSystem>();
		foreach (ParticleSystem particleSystem in componentsInChildren)
		{
			if (particleSystem.name == "BulletHitBodyBlooding")
			{
				ParticleSystem.CollisionModule collision = particleSystem.collision;
				collision.collidesWith = 1 << base.gameObject.layer - Ground.groundTotal;
			}
		}
		UpdateBloodSpriteRenderer();
	}

	public void UpdateBloodSpriteRenderer()
	{
		if ((bool)bloodSpriteRenderer && bloodSpriteRenderer.sprite != bloodSprites[bloodSprites.Length - 1])
		{
			int num = Mathf.FloorToInt(Mathf.Min(damageShare * (float)bloodSprites.Length * 2f, bloodSprites.Length - 1));
			bloodSpriteRenderer.sprite = bloodSprites[num];
		}
	}

	public void OnHit(float damage, Vector2 contactPoint, Vector2 force)
	{
		if (!body.isDead)
		{
			hitCount++;
			if ((bool)body.zombie)
			{
				damageShare = damageCount / body.zombie.health;
			}
			damageCount += damage;
			body.health -= damage;
			if (body.health <= 0f)
			{
				body.Die();
			}
			if (body.isDead && (bool)rb && !dieForceAdded)
			{
				rb.AddForce(force);
				dieForceAdded = true;
			}
		}
	}

	public void OnHit(Weapon weapon, Vector2 direction, Vector2 contactPoint)
	{
		if (body.isDead || weapon == null)
		{
			return;
		}
		hitCount++;
		if ((bool)body.zombie)
		{
			damageShare = damageCount / body.zombie.health;
		}
		body.OnHit(this, weapon, direction, contactPoint);
		if (!(contactPoint == Vector2.zero))
		{
			if ((bool)weapon.bulletFire && weapon.bulletFire.laser == null && weapon.bulletFire.ps == null && hitAudios.Length != 0)
			{
				body.audioSource.PlayOneShot(hitAudios[Random.Range(0, hitAudios.Length)], 2f);
			}
			if (body.health <= 0f)
			{
				body.Die();
			}
			if (body.isDead && base.name == "HeadRb" && weapon.stoppingPower > 50f && Random.Range(0, 2) > 0)
			{
				Explode(contactPoint);
			}
			if (body.isDead && (bool)rb && !dieForceAdded)
			{
				rb.AddForce(weapon.stoppingPower * direction * 3f);
				dieForceAdded = true;
			}
			Bleed(contactPoint);
			if (damageShare > breakAfterDamageShare && body.zombie.isMoveable)
			{
				Break(weapon.stoppingPower * direction * 2f);
			}
		}
	}

	public void Explode(Vector2 pos)
	{
		ParticleSystem.CollisionModule collision = Object.Instantiate(body.bodyExplosionPs, pos, Quaternion.identity).collision;
		collision.collidesWith = 1 << base.gameObject.layer - Ground.groundTotal;
		Object.Destroy(base.gameObject, 0.01f);
		if (base.name == "CalfRb")
		{
			body.zombie.CannotStandUp();
		}
	}

	public void BreakOld(Vector2 force)
	{
		Object.Destroy(hj);
		base.gameObject.transform.SetParent(null);
		if (body.zombie.isStandable)
		{
			body.zombie.animator.Rebind();
		}
		body.zombie.animator.SetBool("isStandable", body.zombie.isStandable);
		bloodSpriteRenderer.sprite = bloodSprites[bloodSprites.Length - 1];
		rb.isKinematic = false;
		rb.AddForce(force);
		rb.AddTorque(10f);
		Staticlize();
		if (base.gameObject.name == "CalfRb")
		{
			body.zombie.CannotStandUp();
		}
		SortingGroup component = GetComponent<SortingGroup>();
		if ((bool)component)
		{
			component.sortingOrder = 0;
		}
	}

	public void Break(Vector2 force)
	{
		if (base.gameObject.name == "CalfRb")
		{
			body.zombie.CannotStandUp();
		}
		Object.Destroy(hj);
		Staticlize();
		SortingGroup component = GetComponent<SortingGroup>();
		if ((bool)component)
		{
			component.sortingOrder = 0;
		}
		BodyPart component2 = Object.Instantiate(base.gameObject, base.transform.position, Quaternion.identity).GetComponent<BodyPart>();
		component2.rb.isKinematic = false;
		component2.rb.AddForce(force);
		component2.rb.AddTorque(10f);
		Object.Destroy(component2.gameObject, 5f);
		Object.Destroy(base.gameObject);
	}

	public void Staticlize()
	{
		Object.Destroy(hj, 5f);
		Object.Destroy(rb, 5f);
		Object.Destroy(col, 5f);
		Object.Destroy(this);
		base.gameObject.tag = "Ground";
	}

	private void OnTriggerEnter2D(Collider2D collision)
	{
		Body component;
		if (collision.name == "KickCollider" && (base.gameObject.name == "ChestRb" || base.gameObject.name == "HeadRb" || base.gameObject.name == "LeftThighRb" || base.gameObject.name == "RightThighRb") && body.zombie.knockbackRestTime <= Time.time)
		{
			float num = 0.5f;
			body.zombie.player.kickCount++;
			if (body.zombie.player.kickCount > body.zombie.player.kickTotal || Mathf.Abs(body.zombie.transform.position.y - body.zombie.player.transform.position.y) > num)
			{
				return;
			}
			if ((bool)body.zombie.animalTrap)
			{
				body.zombie.UnTrapped();
			}
			body.zombie.player.PuchAudio();
			if (!body.zombie.isStandable || (base.gameObject.name == "HeadRb" && body.health / body.zombie.player.strength < 30f))
			{
				body.Die();
				rb.AddForce(body.zombie.player.strength * Vector2.right * 30f + Vector2.right * 10f + Vector2.up * 5f);
				rb.AddTorque(-5f);
				body.chest.GetComponent<Rigidbody2D>().AddForce(body.zombie.player.strength * Vector2.right * 100f);
				Object.Destroy(hj);
				return;
			}
			string animation = null;
			if (knockbackChance > 0f)
			{
				string[] array = new string[2] { "knockback", "knockback" };
				if (body.health < 500f)
				{
					if (body.zombie.player.strength > 0f)
					{
						array[0] = "fallDownBack";
					}
					else if (body.zombie.player.strength > 6f)
					{
						array[0] = "fallDownBack";
						array[1] = "fallDownBack";
					}
				}
				animation = array[Random.Range(0, array.Length)];
			}
			body.zombie.Knockback(body.zombie.player.strength * 200f + 1000f, animation);
		}
		else if (collision.name == "KickCollider" && base.gameObject.name == "ChickenBody" && base.gameObject.TryGetComponent<Body>(out component))
		{
			component.Die();
		}
	}
}
